package com.oa.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oa.common.config.exception.OAException;
import com.oa.common.jwt.JwtHelper;
import com.oa.common.result.Result;
import com.oa.common.utils.MD5;
import com.oa.model.system.SysUser;
import com.oa.system.service.SysMenuService;
import com.oa.system.service.SysUserService;
import com.oa.vo.system.LoginVo;
import com.oa.vo.system.RouterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Admin
 * @title: IndexController
 * @projectName Cloud_OA_Office
 * @description: IndexController
 * @date 2024/10/3 16:53
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    /**
     * 登录
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        //Map<String,Object> map = new HashMap<>();
        //map.put("token","admin");

        String username = loginVo.getUsername();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);

        SysUser sysUser = sysUserService.getOne(wrapper);

        if(sysUser == null){
            throw new OAException(201,"用户不存在");
        }
        //判断密码 数据库存储的密码 与传入的密码MD5加密后进行比对
        if(!MD5.encrypt(loginVo.getPassword()).equals(sysUser.getPassword())){
            throw new OAException(201,"密码错误");
        }
        //判断用户是否被禁用
        if(sysUser.getStatus().intValue() == 0){
            throw new OAException(201,"用户已禁用");
        }
        //3 使用jwt根据用户id 和 名称生成返回token字符串
        String token= JwtHelper.createToken(sysUser.getId(),sysUser.getUsername());

        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        return Result.ok(map);
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public Result info(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        //1 从请求头获取用户信息 获取请求头token字符串
        String token = request.getHeader("token");
        //2 从token字符串获取用户id 和用户名
        Long userId = JwtHelper.getUserId(token);
        //3 根据用户id查询数据库获取用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //4 根据用户id 获取用户可以操作的菜单列表  返回用户可以操作的菜单  按钮
        // 查询数据库 动态构建路由结构 进行显示
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);
        //5 根据用户id 获取用户可以操作的按钮列表
        List<String> permsList = sysMenuService.findUserPermsListByUserId(userId);
        //6 封装用户信息 返回

        map.put("roles","[admin]");
        map.put("name",sysUser.getName());
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        map.put("routers",routerList);
        map.put("buttons",permsList);
        return Result.ok(map);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }
}
